import QtQuick
import QtQuick.Controls

Rectangle {
    color: "lightgray"

    Column {
        anchors.centerIn: parent
        spacing: 20

        Switch {
            id: controlSwitch
            text: "切换组件"
            anchors.horizontalCenter: parent.horizontalCenter
        }

        Loader {
            width: 200
            height: 100
            anchors.horizontalCenter: parent.horizontalCenter
            sourceComponent: controlSwitch.checked ? component1 : component2
        }
    }

    Component {
        id: component1
        Rectangle {
            width: 200
            height: 100
            color: "red"
            Text {
                anchors.centerIn: parent
                text: "组件 1"
                color: "white"
            }
        }
    }

    Component {
        id: component2
        Rectangle {
            width: 200
            height: 100
            color: "blue"
            Text {
                anchors.centerIn: parent
                text: "组件 2"
                color: "white"
            }
        }
    }
}
